import pandas as pd
import re

# Load the Excel file
file_path = "../PointDownLoad/A1底板顶钢筋网.xlsx"
df = pd.read_excel(file_path)


# Split Points column by ';' and then split each coordinate by ','
def split_points(points):
    # Split by ';' to get individual coordinates
    point_list = points.split(';')
    # Remove surrounding parentheses and split each coordinate into x, y, z
    coords = [re.findall(r'-?\d+\.?\d*', point) for point in point_list]
    # Convert coordinates to float and then back to string to ensure correct formatting
    int_coords = [str(int(float(coord))) for point in coords for coord in point]
    return int_coords


# Apply the function to the Points column
expanded_points = df['Points'].apply(split_points)

# Determine the maximum number of points to create the appropriate number of columns
max_points = expanded_points.apply(len).max() // 3

# Create column names for the coordinates
coord_columns = []
for i in range(max_points):
    coord_columns.extend([f'x{i + 1}', f'y{i + 1}', f'z{i + 1}'])

# Expand the points into separate columns
points_df = pd.DataFrame(expanded_points.tolist(), columns=coord_columns)

# Concatenate the new points columns with the original dataframe
final_df = pd.concat([df.drop(columns=['Points']), points_df], axis=1)

# Save the updated dataframe to a new Excel file
output_file_path = "../PointDownLoad/A1底板顶钢筋网_分裂坐标.csv"
final_df.to_csv(output_file_path, index=False)
